package com.ppt.android.base.mvvm.v

import android.content.res.Resources
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.viewbinding.ViewBinding
import com.ppt.android.base.ktx.getAppViewModel
//import com.alibaba.android.arouter.launcher.ARouter
import com.ppt.android.base.mvvm.vm.BaseViewModel
import com.ppt.android.base.mvvm.vm.EventViewModel
import com.ppt.android.base.utils.BindingReflex
import com.ppt.android.base.utils.RegisterEventBus
import com.ppt.android.base.utils.EventBusUtils
import com.ppt.android.base.utils.StateLayoutEnum
import com.ppt.android.base.view.LoadingDialog

/**
 * Fragment基类
 *
 * @author ZouJiaCai
 * @since 8/27/20
 */
abstract class BaseFrameFragment<VB : ViewBinding, VM : BaseViewModel> : Fragment(),
    FrameView<VB> {

    /**
     * 私有的 ViewBinding 此写法来自 Google Android 官方
     */
    private var _binding: VB? = null

    private lateinit var loadingDialog: LoadingDialog

    protected val mBinding get() = _binding!!

    protected abstract val mViewModel: VM

    /**
     * 是否有 [RegisterEventBus] 注解，避免重复调用 [Class.isAnnotation]
     */
    private var mHaveRegisterEventBus = false

    /**
     * 全局唯一的事件viewModel
     */
    protected val eventViewModel: EventViewModel by lazy { getAppViewModel() }

    val screenWidthDp: Int
        get() {
            val displayMetrics = Resources.getSystem().displayMetrics
            return (displayMetrics.widthPixels / displayMetrics.density).toInt()
        }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        _binding = BindingReflex.reflexViewBinding(javaClass, layoutInflater)
        return _binding?.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        // 根据子类是否有 RegisterEventBus 注解決定是否进行注册 EventBus
        if (javaClass.isAnnotationPresent(RegisterEventBus::class.java)) {
            mHaveRegisterEventBus = true
            EventBusUtils.register(this)
        }
        _binding?.initView()
        initObserve()
        initRequestData()
        showLoading()
    }

    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }

    override fun onDestroy() {
        // 根据子类是否有 RegisterEventBus 注解决定是否进行注册 EventBus
        if (mHaveRegisterEventBus) {
            EventBusUtils.unRegister(this)
        }
        super.onDestroy()
    }

    private fun showLoading(){
        mViewModel.status.observe(viewLifecycleOwner) {
            when (it) {
                StateLayoutEnum.HIDE -> {
                    hideLoadingDialog()
                }

                StateLayoutEnum.LOADING -> {
                    showLoadingDialog()
                }

                StateLayoutEnum.ERROR -> {
                    hideLoadingDialog()
                }

                StateLayoutEnum.NO_DATA -> {

                }

                StateLayoutEnum.SUCCESS -> {

                }
            }
        }
    }

    private fun showLoadingDialog() {
        if (!::loadingDialog.isInitialized) {
            loadingDialog = activity?.let { LoadingDialog(it) }!!
        }
        loadingDialog.show()
    }

    private fun hideLoadingDialog() {
        if (::loadingDialog.isInitialized && loadingDialog.isShowing) {
            loadingDialog.dismiss()
        }
    }
}